package com.kmxd.ams.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.system.ISysMenuSvc;
import com.kmxd.ams.client.system.dto.SysMenuAddDTO;
import com.kmxd.ams.client.system.dto.SysMenuEditDTO;
import com.kmxd.ams.client.system.dto.SysMenuQueryDTO;
import com.kmxd.ams.client.system.vo.RouteItemVO;
import com.kmxd.ams.client.system.vo.SysMenuListVO;
import com.kmxd.ams.client.system.vo.SysMenuVO;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 菜单表 前端控制器
 *
 * @author admin
 * @since 2021-06-19
 */
@Tag(name = "菜单管理")
@RestController
@RequestMapping("/system/sysMenu")
@AllArgsConstructor
public class SysMenuCtl {

  private ISysMenuSvc sysMenuSvc;

  /** 分页查询 */
  @GetMapping("/selectPage")
  @AutoLog(module = "菜单管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "分页查询")
  public R<Page<SysMenuListVO>> selectPage(Page reqPage, SysMenuQueryDTO req) {
    return R.success(sysMenuSvc.selectPage(reqPage, req));
  }

  /** 新增 */
  @PostMapping("/add")
  @SaCheckPermission("system:sysMenu:add")
  @AutoLog(module = "菜单管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "新增")
  public R<Void> add(@Validated @RequestBody SysMenuAddDTO req) {
    sysMenuSvc.add(req);
    return R.success();
  }

  /** 修改 */
  @PostMapping("/edit")
  @SaCheckPermission("system:sysMenu:edit")
  @AutoLog(module = "菜单管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "修改")
  public R<Void> edit(@Validated @RequestBody SysMenuEditDTO req) {
    sysMenuSvc.edit(req);
    return R.success();
  }

  /** 删除 */
  @PostMapping("/remove")
  @SaCheckPermission("system:sysMenu:remove")
  @AutoLog(module = "菜单管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH_RISK, title = "删除")
  public R<Void> remove(@RequestParam String ids) {
    sysMenuSvc.remove(ids);
    return R.success();
  }

  /** 查看 */
  @GetMapping("/view")
  @AutoLog(module = "菜单管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查看")
  public R<SysMenuVO> view(@RequestParam Long id) {
    return R.success(sysMenuSvc.view(id));
  }

  /** 查询菜单树 */
  @GetMapping("/listMenuTree")
  @AutoLog(module = "菜单管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查询菜单树")
  public R<List<Tree<String>>> listMenuTree() {
    return R.success(sysMenuSvc.listMenuTree());
  }

  /** 查询下拉菜单树，排除当前节点以及子节点 */
  @GetMapping("/selectMenuTreeAndExcludeNode")
  @AutoLog(
      module = "菜单管理",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.LOW,
      title = "查询下拉菜单树，排除当前节点以及子节点")
  public R<List<Tree<String>>> selectMenuTreeAndExcludeNode(
      @RequestParam(required = false) String nodeId) {
    return R.success(sysMenuSvc.selectMenuTreeAndExcludeNode(nodeId));
  }

  /** 查询下拉菜单树 */
  @GetMapping("/selectMenuTree")
  @AutoLog(module = "菜单管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查询下拉菜单树")
  public R<List<Tree<String>>> selectMenuTree() {
    return R.success(sysMenuSvc.selectMenuTree());
  }

  /** 获取菜单集合 */
  @GetMapping("/getMenuList")
  @AutoLog(module = "菜单管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "获取菜单集合")
  public R<List<RouteItemVO>> getMenuList() {
    return R.success(sysMenuSvc.getMenuList());
  }
}
